<template>
  <div class="welcome">
    <img class="logo anim-fadeIn" :src="logo">
    <p v-if="!isWX">请在微信浏览器打开</p>
  </div>
</template>

<script>
  import logo from './images/loading.gif'
  import $ from 'edom'
  export default {
    name: 'authorize',
    data () {
      return {
        logo: logo,
        isWX: true
      }
    },
    methods: {
      login () {
        let webUrl = 'http://ai.iasfq.com/api/login/index'
        window.location.href = webUrl
      },
      // getUserInfo () {
      //   this.axios.get('/api/login/userInfo')
      //   .then((res) => {
      //     let data = res.data
      //     if (data.code === 403) {
      //       this.login()
      //     } else {
      //       this.$store.commit('loginIn', data.desc)
      //       // 获取首页轮播图
      //       this.api.getBannerImg()
      //       // 获取首页推荐商家
      //       if (this.$store.state.indexBusinessLists.length === 0) {
      //         this.api.getBusinessLists((result) => {
      //           this.$store.commit('cacheIndexBusinessLists', result.recommend_list)
      //         })
      //       }
      //       // 获取个人中心的信息
      //       this.api.getUserInfo()
      //       setTimeout(() => {
      //         let beforeLoginUrl = $().getCookies('beforeLoginUrl')
      //         // let bindseller = $().getCookies('detailId')
      //         let bindseller = localStorage.getItem('detailId')
      //         if (bindseller) {
      //           this.$store.commit('bindseller', bindseller)
      //           this.$router.push('/detail/' + bindseller)
      //           return
      //         }
      //         if (beforeLoginUrl) {
      //           this.$router.push(beforeLoginUrl)
      //         } else {
      //           this.$router.push('/index')
      //         }
      //       }, 1000)
      //     }
      //   })
      // }
      // ==================================================
      getUserInfo () {
        this.axios.get('/api/login/getAuth')
        .then((res) => {
          let data = res.data
          if (data.code === 200) {
            let openid = data.desc
            this.axios.post('/api/login/userInfo', {openid: openid})
            .then((res) => {
              let data = res.data
              if (data.code === 403) {
                this.login()
              } else {
                this.$store.commit('loginIn', data.desc)
                // 获取首页轮播图
                this.api.getBannerImg()
                // 获取首页推荐商家
                if (this.$store.state.indexBusinessLists.length === 0) {
                  this.api.getBusinessLists((result) => {
                    this.$store.commit('cacheIndexBusinessLists', result.recommend_list)
                  })
                }
                // 获取个人中心的信息
                this.api.getUserInfo()
                setTimeout(() => {
                  let beforeLoginUrl = $().getCookies('beforeLoginUrl')
                  // let bindseller = $().getCookies('detailId')
                  let bindseller = localStorage.getItem('detailId')
                  if (bindseller) {
                    // alert('分享' + bindseller)
                    localStorage.clear()
                    this.$store.commit('bindseller', bindseller)
                    this.$router.push('/detail/' + bindseller)
                    return
                  }
                  if (beforeLoginUrl) {
                    // alert('url' + beforeLoginUrl)
                    this.$router.push(beforeLoginUrl)
                  } else {
                    this.$router.push('/index')
                  }
                }, 1000)
              }
            })
          } else {
            this.login()
          }
        })
      }
    },
    created () {
      let ua = window.navigator.userAgent.toLowerCase()
      if (ua.indexOf('micromessenger') === -1) {
        // 不是微信环境
        this.isWX = false
        return
      }
      this.getUserInfo()
    }
  }
</script>

<style lang="scss" scoped>
  .welcome{
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: rgb(251, 251, 251);
    font-size: 16px;
    text-align: center;
    color: #999;
  }
  .logo{
    display: block;
    margin: 0 auto;
    padding-top: 70%;
    width: 50%;
  }
</style>

